using System;
using System.Collections;
using Holoville.HOTween;
using UnityEngine;

public class PopupAnimation : MonoBehaviour, IAnimationHandler
{
	public Camera Cam
	{
		get
		{
			return this.cam;
		}
		set
		{
			this.cam = value;
		}
	}

	public GameObject Target
	{
		get
		{
			return this.target;
		}
		set
		{
			this.target = value;
		}
	}

	public GameObject BG
	{
		get
		{
			return this.bg;
		}
		set
		{
			this.bg = value;
		}
	}

	public DIRECTION ShowDirection
	{
		get
		{
			return this.showDirection;
		}
		set
		{
			this.showDirection = value;
		}
	}

	public DIRECTION CloseDirection
	{
		get
		{
			return this.closeDirection;
		}
		set
		{
			this.closeDirection = value;
		}
	}

	public float ShowDuration
	{
		get
		{
			return this.showDuration;
		}
		set
		{
			this.showDuration = value;
		}
	}

	public float CloseDuration
	{
		get
		{
			return this.closeDuration;
		}
		set
		{
			this.closeDuration = value;
		}
	}

	public EaseType ShowEase
	{
		get
		{
			return this.showEase;
		}
		set
		{
			this.showEase = value;
		}
	}

	public EaseType CloseEase
	{
		get
		{
			return this.closeEase;
		}
		set
		{
			this.closeEase = value;
		}
	}

	private float AdjustedShowTime
	{
		get
		{
			return this.showDuration * 0.625f;
		}
	}

	private float AdjustedCloseTime
	{
		get
		{
			return this.closeDuration + this.closeDuration * 0.625f;
		}
	}

	private void Awake()
	{
		this.screenSize = this.GetScreenSize();
		this.originalPos = this.target.transform.localPosition;
	}

	public IEnumerator CoShowAnimation()
	{
		bool isComplete = false;
		this.AnimateShow(delegate
		{
			isComplete = true;
		});
		while (!isComplete)
		{
			yield return null;
		}
		yield break;
	}

	public IEnumerator CoHideAnimation()
	{
		bool isComplete = false;
		this.AnimateClose(delegate
		{
			isComplete = true;
		});
		while (!isComplete)
		{
			yield return null;
		}
		yield break;
	}

	public void AnimateShow(Action onComplete)
	{
		if (this.bg != null)
		{
			this.bg.SetActive(true);
		}
		HOTween.Kill(base.GetInstanceID());
		this.target.transform.localPosition = this.originalPos;
		Sequence sequence = new Sequence(new SequenceParms().IntId(base.GetInstanceID()));
		TweenParms tweenParms = new TweenParms();
		switch (this.showDirection)
		{
		case DIRECTION.UP:
			tweenParms.Prop("localPosition", Vector3.down * this.screenSize.y).Ease(this.showEase, 0.8f, this.AdjustedShowTime);
			sequence.Append(HOTween.From(this.target.transform, this.showDuration, tweenParms));
			break;
		case DIRECTION.DOWN:
			tweenParms.Prop("localPosition", Vector3.up * this.screenSize.y).Ease(this.showEase, 0.8f, this.AdjustedShowTime);
			sequence.Append(HOTween.From(this.target.transform, this.showDuration, tweenParms));
			break;
		case DIRECTION.LEFT:
			tweenParms.Prop("localPosition", Vector3.right * this.screenSize.x).Ease(this.showEase, 0.8f, this.AdjustedShowTime);
			sequence.Append(HOTween.From(this.target.transform, this.showDuration, tweenParms));
			break;
		case DIRECTION.RIGHT:
			tweenParms.Prop("localPosition", Vector3.left * this.screenSize.x).Ease(this.showEase, 0.8f, this.AdjustedShowTime);
			sequence.Append(HOTween.From(this.target.transform, this.showDuration, tweenParms));
			break;
		case DIRECTION.RANDOM:
			tweenParms.Prop("localPosition", this.GetRandomDestination()).Ease(this.showEase, 0.8f, this.AdjustedShowTime);
			sequence.Append(HOTween.From(this.target.transform, this.showDuration, tweenParms));
			break;
		}
		sequence.AppendCallback(delegate(TweenEvent twpm)
		{
			if (Vector3.Distance(this.target.transform.localPosition, this.originalPos) > this.screenSize.magnitude * 0.1f)
			{
				UnityEngine.Debug.Log("FIXME : PopupAnimation Wait 2 frame");
				TaskManager.DoNextFrame(delegate
				{
					if (onComplete != null)
					{
						onComplete();
					}
				}, 2);
			}
			else if (onComplete != null)
			{
				onComplete();
			}
		}, new object[0]);
		sequence.Play();
	}

	public void AnimateClose(Action onComplete)
	{
		if (this.bg != null)
		{
			this.bg.SetActive(false);
		}
		HOTween.Kill(base.GetInstanceID());
		Sequence sequence = new Sequence(new SequenceParms().IntId(base.GetInstanceID()));
		TweenParms tweenParms = new TweenParms();
		switch (this.closeDirection)
		{
		case DIRECTION.UP:
			tweenParms.Prop("localPosition", Vector3.up * this.screenSize.y).Ease(this.closeEase, 0.8f, this.AdjustedCloseTime);
			sequence.Append(HOTween.To(this.target.transform, this.closeDuration, tweenParms));
			break;
		case DIRECTION.DOWN:
			tweenParms.Prop("localPosition", Vector3.down * this.screenSize.y).Ease(this.closeEase, 0.8f, this.AdjustedCloseTime);
			sequence.Append(HOTween.To(this.target.transform, this.closeDuration, tweenParms));
			break;
		case DIRECTION.LEFT:
			tweenParms.Prop("localPosition", Vector3.left * this.screenSize.x).Ease(this.closeEase, 0.8f, this.AdjustedCloseTime);
			sequence.Append(HOTween.To(this.target.transform, this.closeDuration, tweenParms));
			break;
		case DIRECTION.RIGHT:
			tweenParms.Prop("localPosition", Vector3.right * this.screenSize.x).Ease(this.closeEase, 0.8f, this.AdjustedCloseTime);
			sequence.Append(HOTween.To(this.target.transform, this.closeDuration, tweenParms));
			break;
		case DIRECTION.RANDOM:
			tweenParms.Prop("localPosition", this.GetRandomDestination()).Ease(this.closeEase, 0.8f, this.AdjustedCloseTime);
			sequence.Append(HOTween.To(this.target.transform, this.closeDuration, tweenParms));
			break;
		}
		sequence.AppendCallback(delegate(TweenEvent twpm)
		{
			if (onComplete != null)
			{
				onComplete();
			}
		}, new object[0]);
		sequence.Play();
	}

	private Vector2 GetScreenSize()
	{
		Vector3 vector = this.cam.ViewportToWorldPoint(new Vector3(0f, 0f, this.cam.nearClipPlane));
		Vector3 vector2 = this.cam.ViewportToWorldPoint(new Vector3(1f, 1f, this.cam.nearClipPlane));
		return new Vector2(vector2.x - vector.x, vector2.y - vector.y);
	}

	private Vector3 GetRandomDestination()
	{
		int num = UnityEngine.Random.Range(0, this.directions.Length);
		return this.directions[num] * ((num <= 1) ? this.screenSize.y : this.screenSize.x);
	}

	[SerializeField]
	private Camera cam;

	[SerializeField]
	private GameObject target;

	[SerializeField]
	private GameObject bg;

	[SerializeField]
	private DIRECTION showDirection;

	[SerializeField]
	private DIRECTION closeDirection;

	[SerializeField]
	private float showDuration = 0.7f;

	[SerializeField]
	private float closeDuration = 0.3f;

	[SerializeField]
	private EaseType showEase = EaseType.EaseOutElastic;

	[SerializeField]
	private EaseType closeEase = EaseType.EaseInElastic;

	private Vector3[] directions = new Vector3[]
	{
		Vector3.up,
		Vector3.down,
		Vector3.left,
		Vector3.right
	};

	private Vector3 originalPos;

	private Vector2 screenSize;

	private const float timeAdjustment = 0.625f;
}
